package com.example.yblin.hello24;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yblin.hello24.DataUtils.GameUtil;
import com.example.yblin.hello24.DataUtils.LocalDataUtil;
import com.example.yblin.hello24.DataUtils.NetUtil;

/**
 * 引导到其他页面
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout btStart;
    private LinearLayout btStageStart;
    private LinearLayout btHelp;
    private LinearLayout btRanklist;
    private LinearLayout btSwitch;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btStageStart = findViewById(R.id.button_stage_start);
        btStart = findViewById(R.id.button_start);
        btHelp = findViewById(R.id.button_help);
        btRanklist = findViewById(R.id.button_ranklist);
        btSwitch = findViewById(R.id.button_exit);
        tvTitle = findViewById(R.id.textView_title);
        tvTitle.setText(LocalDataUtil.read(this, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME));

        btStageStart.setOnClickListener(this);
        btStart.setOnClickListener(this);
        btHelp.setOnClickListener(this);
        btRanklist.setOnClickListener(this);
        btSwitch.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_stage_start:
                new StageGameStartAsync().execute();
                btStageStart.setEnabled(false);
                break;
            case R.id.button_start:
                new GameStartAsync().execute();
                btStart.setEnabled(false);
                break;
            case R.id.button_help:
                startActivity(new Intent(this, HelpActivity.class));
                break;
            case R.id.button_ranklist:
                startActivity(new Intent(this, RankActivity.class));
                break;
            case R.id.button_exit:
                startActivity(new Intent(this, LoginActivity.class));
                break;
        }
    }

    public class GameStartAsync extends AsyncTask<Void, Void, Integer> {
        @Override
        protected Integer doInBackground(Void... voids) {
            return GameUtil.start(MainActivity.this);
        }

        @Override
        protected void onPostExecute(Integer result) {
            btStart.setEnabled(true);
            if (result == NetUtil.CODE_OK) {
                startActivity(new Intent(MainActivity.this, GameActivity.class));
            }
        }
    }


    public class StageGameStartAsync extends AsyncTask<Void, Void, Integer> {
        @Override
        protected Integer doInBackground(Void... voids) {
            return GameUtil.stageStart(MainActivity.this);
        }

        @Override
        protected void onPostExecute(Integer result) {
            btStageStart.setEnabled(true);
            if (result == NetUtil.CODE_OK) {
                startActivity(new Intent(MainActivity.this, StageGameActivity.class));
            }
        }
    }
}